package mydessertstore.web.servlet;

import mydessertstore.domain.Product;
import mydessertstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;

public class ProductAutoCompleteServlet extends HttpServlet {
    @Override
    //@RequestMapping(value = "/reg.htm", method = RequestMethod.POST, produces = "text/html; charset=UTF-8")

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String keyword = new String(req.getParameter("keyword").getBytes("iso-8859-1"), "utf-8");
        CatalogService catalogService = new CatalogService();
        List<Product> productsBySearch = new ArrayList<Product>();
        productsBySearch = catalogService.searchProductList(keyword);

        String result = JSON.toJSONString(productsBySearch);
        System.out.println(result);

        resp.setContentType("text/json;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        PrintWriter out = resp.getWriter();
        out.println(result);
    }
}
